﻿ 
using System.Collections;
using System.Collections.Generic;
using UnityEditor; 
using UnityEngine;
using XFGameFramework.Localization;

public class MultiLanguageConfigsController 
{

    private const string LANGUAGE_CONFIG_DATA_PATH = "Assets/Extensions/Localization/{0}";

    private static List<MultiLanguageConfigs> configs = new List<MultiLanguageConfigs>();

    public static List<MultiLanguageConfigs> GetAllMultiLanguageConfigs()
    {
        configs.Clear();

        string[] guids = AssetDatabase.FindAssets(string.Format("t:{0}", typeof(MultiLanguageConfigs).FullName));
        if (guids == null || guids.Length == 0)
            return configs;

        foreach (var item in guids)
        {
            string assetpath = AssetDatabase.GUIDToAssetPath(item);

            if (string.IsNullOrEmpty(assetpath))
                continue;
            MultiLanguageConfigs config = AssetDatabase.LoadAssetAtPath<MultiLanguageConfigs>(assetpath);
            if (config == null) continue;
            configs.Add(config);
        }

        return configs;
    }

    public static bool IsContainID(int id) 
    {
        List<MultiLanguageConfigs> c = GetAllMultiLanguageConfigs();

        foreach (var item in c)
        {
            foreach (var languageConfig in item.Languages)
            {
                if (languageConfig.GetLanguageInfo(id) == null)
                {
                    // 同一个配置文件的 不同语言内的id信息都是一样的 所以只需要判断一个
                    break;
                }
                else {
                    return true;
                }
            }
        }

        return false;
    }


    public static void CreateLanguageConfig(SystemLanguage language, MultiLanguageConfigs multiLanguageConfigs)
    { 
        if (multiLanguageConfigs == null) return;
      
        LanguageConfig last = null;

        for (int i = 0; i < multiLanguageConfigs.Languages.Count; i++)
        {
            if (multiLanguageConfigs.Languages[0] == null)
                continue;

            last = multiLanguageConfigs.Languages[i];
            break;
        }

        LanguageConfig languageConfig = ScriptableObject.CreateInstance<LanguageConfig>();
        languageConfig.language = language;

        if (last != null)
        {
            // 同步语言配置 
            foreach (var item in last.languagesInfo)
            {
                LanguageInfo info = new LanguageInfo();
                info.id = item.id;
                info.type = item.type;
                languageConfig.languagesInfo.Add(info);
            }
        }
         
        string path = GetLanguageConfigDataPath(multiLanguageConfigs);
        CreateAsset(languageConfig, path, false);
        multiLanguageConfigs.Languages.Add(languageConfig);
        EditorUtility.SetDirty(multiLanguageConfigs);
        multiLanguageConfigs.ClearCache();
        languageConfig.Rename();
        EditorUtility.SetDirty(languageConfig);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
         
    }

    public static string GetLanguageConfigDataPath(MultiLanguageConfigs multiLanguageConfigs) 
    {
        string asset_path = AssetDatabase.GetAssetPath(multiLanguageConfigs);
        string guid = AssetDatabase.AssetPathToGUID(asset_path);

        return string.Format("Assets/Extensions/Localization/{0}", guid);
    }

    public static void CreateAsset(ScriptableObject scriptableObject, string path, bool ping = true)
    {
        if (!System.IO.Directory.Exists(path))
        {
            System.IO.Directory.CreateDirectory(path);
            AssetDatabase.Refresh();
        }

        string name = GetName(scriptableObject.GetType().Name, path);
        AssetDatabase.CreateAsset(scriptableObject, string.Format("{0}/{1}.asset", path, name));
        Selection.activeObject = scriptableObject;
        if (ping)
        { 
            EditorGUIUtility.PingObject(scriptableObject); 
        }

        if (!Application.isPlaying)
        {
            LocalizationManager.Configs.Clear();
        }
    }

    private static string GetName(string name, string path)
    {

        if (!AssetDatabase.IsValidFolder(path))
            return name;

        string tempName = name;

        string[] matchs = AssetDatabase.FindAssets(string.Format("t:ScriptableObject {0}", tempName), new string[] { path });

        int count = 0;

        while (matchs != null && matchs.Length > 0)
        {
            count++;


            tempName = string.Format("{0} ({1})", name, count);
            matchs = AssetDatabase.FindAssets(string.Format("t:ScriptableObject {0}", tempName), new string[] { path });
        }


        return tempName;
    }


}
